package wicresoft.controller.jianYan;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.BusinessMajorServices;
import services.dictionary.MajorTypeServices;
import services.staff.StaffInfoServices;
import util.JsonUtil;
import wicresoft.controller.common.CommonResult;
import wicresoft.controller.common.Properties;
import wicresoft.controller.common.UserIdHandle;
import wicresoft.orm.entity.JyCheckPlan;
import wicresoft.orm.entity.JyExpertGroup;
import wicresoft.services.jianYan.JyCheckPlanServices;
import wicresoft.services.jianYan.JyExpertGroupServices;
import wt.entity.dictionary.MajorType;
import wt.webPlugin.ZTree;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/12.
 */
@Controller
@RequestMapping("httpPage")
public class JianYanAddBusinessController {
    @Autowired
    JyCheckPlanServices jyCheckPlanServices;
    @Autowired
    JyExpertGroupServices jyExpertGroupServices;
    @Autowired
    UserIdHandle userIdHandle;
    @Autowired
    BusinessInfoServices businessInfoServices;

    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    BusinessMajorServices businessMajorServices;
    @Autowired
    StaffInfoServices staffInfoServices;


    @RequestMapping(value = "addBusinessPage")
    public String addBusinessPage(@RequestParam(value = "item_id") String checkPlanId,
                                  @RequestParam(value = "manager_person") String managerUserId,
                                  @RequestParam(value = "observer_id") String observerUserId,
                                  @RequestParam(value = "major") String oldMajor, Map map) throws Exception {
        JyCheckPlan jyCheckPlan = jyCheckPlanServices.selectById(checkPlanId);
        if (jyCheckPlan == null) {
            map.put("errorMessage", "item_id 数据无效");
            return "common/errorPage";
        }


        if (businessInfoServices.selectByBusinessCode(jyCheckPlan.getAcceptedId()) != null) {//业务已经存在，直接跳转到抽取。
            return "redirect:extractExpertPage?checkPlanId=" + checkPlanId + "&businessCode=" + jyCheckPlan.getAcceptedId();
        } else {
            managerUserId = userIdHandle.getNewUserID(managerUserId);
            if (jyCheckPlan == null) {
                map.put("errorMessage", "manager_person 数据无效");
                return "common/errorPage";
            }

//            StaffInfo staffInfo = staffInfoServices.selectByUserId(observerUserId);
//            if (staffInfo == null) {
//                map.put("errorMessage", "observer_id 数据无效");
//                return "common/errorPage";
//            }


            map.put("businessCode", jyCheckPlan.getAcceptedId());
            map.put("companyName", jyCheckPlan.getApplyName());
            map.put("companyCode", jyCheckPlan.getApplyOrg());
            map.put("address", jyCheckPlan.getLabAddr());
            map.put("productName", null);
            map.put("businessType", jyCheckPlan.getName().substring(4));
            map.put("applicationTime", jyCheckPlan.getApplyTime());
            map.put("acceptTime", jyCheckPlan.getSendTime());
            map.put("itemNum", "35");
            map.put("managerUserId", managerUserId);
//            map.put("observerUserId", observerUserId);

            map.put("oldMajor", oldMajors(oldMajor));
            return "jianYan/businessInfo/addBusiness";
        }

    }

    @ResponseBody
    @RequestMapping(value = "addBusiness", produces = "application/json;charset=UTF-8")
    public Object addBusiness(String businessCode, String companyName, String companyCode, String address,
                              String productName, String businessType, String applicationTime, String acceptTime,
                              String itemNum, String managerUserId, String observerUserId, String majorNumList) throws Exception {
        Map<String, Object> data = new HashMap();
        data.put("businessCode", businessCode);
        data.put("companyName", companyName);
        data.put("companyCode", companyCode);
        data.put("address", address);
        data.put("productName", productName);
        data.put("businessType", businessType);
        data.put("applicationTime", applicationTime);
        data.put("acceptTime", acceptTime);
        data.put("itemNum", itemNum);
        data.put("managerUserId", managerUserId);
        data.put("observerUserId", observerUserId);

        data.put("majorNumList", majorNumList);
        return Properties.sendData(Properties.addBusinessInfo, data, Properties.uid, Properties.key);

    }


    @ResponseBody
    @RequestMapping(value = "addBusinessExpert", produces = "application/json;charset=UTF-8")
    public Object addExpert(String item_id, String startTime, String endTime) throws Exception {

        JyCheckPlan jyCheckPlan = jyCheckPlanServices.selectById(item_id);
        if (jyCheckPlan == null) {
            CommonResult commonResult = new CommonResult();
            commonResult.setCode(CommonResult.ZJ005);
            commonResult.setMessage("item_id 未找到数据");
            return commonResult;
        } else {
            List<JyExpertGroup> jyExpertGroupList = jyExpertGroupServices.selectByApplyId(item_id);
            if (jyExpertGroupList.isEmpty()) {
                CommonResult commonResult = new CommonResult();
                commonResult.setCode(CommonResult.ZJ005);
                commonResult.setMessage("专家还未抽取");
                return commonResult;
            } else {
                String leaderIdCard = null, crewIdCardList = "";
                for (JyExpertGroup jyExpertGroup : jyExpertGroupList) {
                    if ("ybdd_common_dic_export_dute_01".equals(jyExpertGroup.getDuty())) {//组长
                        leaderIdCard = jyExpertGroup.getIdCard();
                    } else {
                        crewIdCardList = crewIdCardList + "," + jyExpertGroup.getIdCard();
                    }
                }
                if (crewIdCardList.indexOf(",") == 0) {
                    crewIdCardList = crewIdCardList.substring(1);
                }
                Map<String, Object> data = new HashMap();
                data.put("businessCode", jyCheckPlan.getAcceptedId());
                data.put("leaderIdCard", leaderIdCard);
                data.put("crewIdCardList", crewIdCardList);
                data.put("startTime", startTime);
                data.put("endTime", endTime);
                return Properties.sendData(Properties.addBusinessExperts, data, Properties.uid, Properties.key);
            }
        }
    }


    @ResponseBody
    @RequestMapping(value = "addBusinessResult", produces = "application/json;charset=UTF-8")
    public Object addBusinessResult(String item_id, String startTime, String endTime, String result) throws Exception {

        JyCheckPlan jyCheckPlan = jyCheckPlanServices.selectById(item_id);
        if (jyCheckPlan == null) {
            CommonResult commonResult = new CommonResult();
            commonResult.setCode(CommonResult.ZJ005);
            commonResult.setMessage("item_id 未找到数据");
            return commonResult;
        } else {
            Map<String, Object> data = new HashMap();
            data.put("businessCode", jyCheckPlan.getAcceptedId());
            data.put("result", result);
            data.put("startTime", startTime);
            data.put("endTime", endTime);
            return JsonUtil.json2Obj(Properties.sendData(Properties.addBusinessResult, data, Properties.uid, Properties.key), Map.class);
        }
    }


    /**
     * 获取专业树
     *
     * @param
     * @param
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "getProfessionTree", produces = "application/json;charset=UTF-8")
    public Object getProfessionSetTree() throws Exception {
//        369000000000004171               35
        //369000000000004167               43
        List<MajorType> majorTypeList = majorTypeServices.selectMajorType("369000000000004171", true);
        JSONArray jsonArray = new JSONArray();
        for (MajorType majorType : majorTypeList) {
            JSONObject jsonObject = ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName(), majorType.getNum());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }


    public static String oldMajors(String key) {

        if ("rz_jlrz_speciality_type_z".equals(key)) {
            return "00-食品";
        } else if ("rz_jlrz_speciality_type_l".equals(key)) {
            return "01-建筑工程";
        } else if ("rz_jlrz_speciality_type_m".equals(key)) {
            return "02-建材";
        } else if ("rz_jlrz_speciality_type_s".equals(key)) {
            return "03-卫生";
        } else if ("rz_jlrz_speciality_type_d".equals(key)) {
            return "04-农牧渔业";
        } else if ("rz_jlrz_speciality_type_x".equals(key)) {
            return "05-机动车安检";
        } else if ("rz_jlrz_speciality_type_w".equals(key)) {
            return "06-公安刑事技术";
        } else if ("rz_jlrz_speciality_type_g".equals(key)) {
            return "07-司法鉴定";
        } else if ("rz_jlrz_speciality_type_b".equals(key)) {
            return "08-机械";
        } else if ("rz_jlrz_speciality_type_u".equals(key)) {
            return "09-电子信息";
        } else if ("rz_jlrz_speciality_type_a".equals(key)) {
            return "10-轻工";
        } else if ("rz_jlrz_speciality_type_y".equals(key)) {
            return "11-纺织服装";
        } else if ("rz_jlrz_speciality_type_r".equals(key)) {
            return "12-环境与环保";
        } else if ("rz_jlrz_speciality_type_p".equals(key)) {
            return "13-水质";
        } else if ("rz_jlrz_speciality_type_k".equals(key)) {
            return "14-化工";
        } else if ("rz_jlrz_speciality_type_t".equals(key)) {
            return "15-医疗器械";
        } else if ("rz_jlrz_speciality_type_v".equals(key)) {
            return "16-采矿冶金";
        } else if ("rz_jlrz_speciality_type_c".equals(key)) {
            return "17-能源";
        } else if ("rz_jlrz_speciality_type_j".equals(key)) {
            return "18-医学";
        } else if ("rz_jlrz_speciality_type_q".equals(key)) {
            return "19-生物安全";
        } else if ("rz_jlrz_speciality_type_f".equals(key)) {
            return "20-综合";
        } else if ("rz_jlrz_speciality_type_n".equals(key)) {
            return "21-其他";
        } else {
            return null;
        }
    }

}
